<?php

class Admin_Table_Products extends Apk_Db_Table
{
	protected $_name      = 'products';
    protected $_primary   = 'id';
    protected $_ERROR = '';

    public function GetError()
    {
    	return $this->_ERROR;
    }

    public function GetDataList($page=1, $sort=null, $page_count=null, $params=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'title_ru', 'categories.menu_name_ru', 'is_active', 'is_top'))
    		  ->join('categories', 'categories.id = products.id_cat', array())
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0');

		if(count($params) > 0)
		foreach($params as $k => $v)
		{
			switch ($k)
			{
				case 'cats':
					if(count($v) > 0)
					$sel->where('id_cat in (?)', $v);
					break;
				case 'is_top':
				case 'is_active':
					if(intval($v) >= 0 && trim($v) != '')
					$sel->where($this->_name.'.'.$k.' = ?', intval($v));
					break;
				default:
					if(intval($v) > 0)
					$sel->where($this->_name.'.'.$k.' = ?', $v);
			}
		}


		if(!is_null($sort) && strlen($sort) > 0) $sel->order($this->_name.'.'.$sort);
		else $sel->order($this->_name.'.public_date desc');

    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }

    public function GetChildsList($id, $page=1, $sort=null, $page_count=null, $params=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('products_rel', array())
    		  ->join($this->_name, 'products.id = products_rel.id_child',array('id', 'title', 'public_date','is_active'))
			  ->where('products_rel.id_parent = ?', $id)
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0');



		if(count($params) > 0)
		foreach($params as $k => $v)
		{
			switch ($k)
			{
				case 'cats':
					if(count($v) > 0)
					$sel->where('id_cat in (?)', $v);
					break;
				case 'is_top':
				case 'is_active':
					if(intval($v) >= 0 && trim($v) != '')
					$sel->where($this->_name.'.'.$k.' = ?', intval($v));
					break;
				default:
					if(intval($v) > 0)
					$sel->where($this->_name.'.'.$k.' = ?', $v);
			}
		}


		if(!is_null($sort) && strlen($sort) > 0) $sel->order($this->_name.'.'.$sort);
		else $sel->order($this->_name.'.public_date desc');

    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }

    public function GetProducts($id, $page=1,  $params=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'title', 'public_date','is_active', 'is_top'))
    		  ->joinLeft('products_rel', 'products_rel.id_child = products.id and products_rel.id_parent = '.$id, array('is_child' => 'id_child'))
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0')
    		  ->order($this->_name.'.public_date desc');

		if(count($params) > 0)
		foreach($params as $k => $v)
		{
			switch ($k)
			{
				case 'cats':
					if(count($v) > 0)
					$sel->where('id_cat in (?)', $v);
					break;
				case 's':
					if(trim($v) != '')
					$sel->where('title like "%'.$v.'%"');
					break;
				case 'is_top':
				case 'is_active':
					if(intval($v) >= 0 && trim($v) != '')
					$sel->where($this->_name.'.'.$k.' = ?', intval($v));
					break;
				default:
					if(intval($v) > 0)
					$sel->where($this->_name.'.'.$k.' = ?', $v);
			}
		}

		//echo $sel; die();


    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => 30));
    }

    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('id = ?', $id);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }

	public function GetProdList($a = null, $params = array())
    {
    	$ret = array();

		$sel = $this->getAdapter()->select()
					->from($this->_name, array('id', 'title_ru'))
					->where($this->_name.'.is_removed = 0')
					->where($this->_name.'.is_ghost = 0')
					->where($this->_name.'.is_active = 1')
					->order($this->_name.'.title_ru');

		if(count($params) > 0)
		foreach($params as $k => $v)
		{
			switch ($k)
			{
				case 'cats':
					if(count($v) > 0)
					$sel->where('id_cat in (?)', $v);
					break;
				case 'is_top':
				case 'is_active':
					if(intval($v) >= 0 && trim($v) != '')
					$sel->where($this->_name.'.'.$k.' = ?', intval($v));
					break;
				default:
					if(intval($v) > 0)
					$sel->where($this->_name.'.'.$k.' = ?', $v);
			}
		}

		//echo $sel; die();


		$data = $this->getList($sel)->toArray();

    	if(count($data) > 0 && !is_null($a))
    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }

    public function GetLangList($a=null)
    {
    	$sel = $this->getAdapter()->select()
    			->from('languages')
    			->where('is_active')
    			->order('id');
    	$data = $this->getList($sel)->toArray();

    	if($a > 0)
    	foreach ($data as $k => $v)
    		if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }


    //*******************
    // insert/update/delete

	public function insert(array $data, $user_id)
    {
    	$data['public_date'] = time();
    	$data['modify_date'] = time();
    	$data['add_date'] = time();
    	$data['add_user_id'] = $user_id;
    	$data['modify_user_id'] = $user_id;

        return parent::insert($data);
    }

    public function update(array $data, $id, $user_id)
    {
    	$data['modify_date'] = time();
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;

    	if(!isset($data['is_active'])) $data['is_active'] = 0;
    	if(!isset($data['is_top'])) $data['is_top'] = 0;
    	if(!isset($data['is_show_gallery'])) $data['is_show_gallery'] = 0;

		return parent::update($data, 'id = '.$id);
    }

    public function update_all(array $data, $id, $user_id)
    {
    	$data['modify_date'] = time();
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;

		return parent::update($data, 'id = '.$id);
    }

    public function remove($items, $user_id)
    {
    	foreach($items as $k => $id)
    	{
	    	$data['modify_user_id'] = $user_id;
	    	$data['modify_date'] = time();
	    	$data['is_removed'] = 1;

	    	parent::update($data, 'id = '.$id);
    	}

    	return true;
	}

	public function addChild($id_parent, $id_child)
    {
    	$data['id_parent'] = $id_parent;
    	$data['id_child'] = $id_child;

        try{
    		$this->getAdapter()->insert('products_rel', $data);
        }catch(Exception $e)
        {
        	;
        }
    }

	public function removeChild($id_parent, $id_child)
    {
    	$this->getAdapter()->delete('products_rel', 'id_parent = '.$id_parent.' and id_child = '.$id_child);
    }

    public function removeChilds($id_parent, $items)
    {
    	foreach($items as $k => $id)
    	{
	    	$this->removeChild($id_parent, $id);

    	}

    	return true;
	}


    public function photo_count($id)
    {
    	$sel = $this->getAdapter()->select()
    					->from('objects', array('count' => 'count(id)'))
    					->where('is_active')
    					->where('is_ghost = 0')
    					->where('is_removed = 0')
    					->where('id_article = ?', $id);

    	$count = $this->getList($sel)->getRow(0)->toArray();

    	$d['count_photo'] = $count['count'];
    	parent::update($d, 'id = '.$id);
    }


    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }



}
?>